using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;

namespace DatabaseManager.Util.Exporters
{
    class DataExportXml : DataExportBase
    {
        public override event DataExportProcessHandler DataExportProcess;

        public DataExportXml(DataTable dataTable, string fileName)
            : base(dataTable, fileName) { }

        public override void Export()
        {
            int currentDataRowIndex = 0;
            int dataRowIndexCount = _dataTable.Rows.Count;
            
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;

            XmlWriter xmlWriter = XmlWriter.Create(_fileName, xmlWriterSettings);
            
            xmlWriter.WriteStartDocument();

            xmlWriter.WriteStartElement("DataExport");

            //Write columns
            xmlWriter.WriteStartElement("Columns");
            foreach (DataColumn dataColumn in _dataTable.Columns)
            { 
                xmlWriter.WriteStartElement("Column");
                
                xmlWriter.WriteStartAttribute("columnIndex");
                xmlWriter.WriteValue(dataColumn.Ordinal);
                xmlWriter.WriteEndAttribute();

                xmlWriter.WriteStartAttribute("columnType");
                xmlWriter.WriteValue(dataColumn.DataType.FullName);
                xmlWriter.WriteEndAttribute();

                xmlWriter.WriteStartAttribute("allowNull");
                xmlWriter.WriteValue(dataColumn.AllowDBNull);
                xmlWriter.WriteEndAttribute();

                xmlWriter.WriteStartAttribute("isUnique");
                xmlWriter.WriteValue(dataColumn.Unique);
                xmlWriter.WriteEndAttribute();

                xmlWriter.WriteStartAttribute("autoIncrement");
                xmlWriter.WriteValue(dataColumn.AutoIncrement);
                xmlWriter.WriteEndAttribute();


                xmlWriter.WriteStartElement("ColumnName");
                xmlWriter.WriteValue(dataColumn.ColumnName);
                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndElement();
            }
            xmlWriter.WriteEndElement();

            //Write data
            xmlWriter.WriteStartElement("Rows");
            foreach (DataRow dataRow in _dataTable.Rows)
            {
                currentDataRowIndex++;

                xmlWriter.WriteStartElement("Row");

                foreach (DataColumn dataColumn in _dataTable.Columns)
                {
                    xmlWriter.WriteStartElement("Data");
                    
                    xmlWriter.WriteStartAttribute("columnIndex");
                    xmlWriter.WriteValue(dataColumn.Ordinal);
                    xmlWriter.WriteEndAttribute();

                    xmlWriter.WriteStartAttribute("columnName");
                    xmlWriter.WriteValue(dataColumn.ColumnName);
                    xmlWriter.WriteEndAttribute();

                    string value = ObjectToString(dataRow[dataColumn]);

                    xmlWriter.WriteStartElement("Value");
                    xmlWriter.WriteValue(value);
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteEndElement();
                }
                
                xmlWriter.WriteEndElement();

                if (DataExportProcess != null)
                { 
                    DataExportProcess(
                        this,
                        new DataExportProcessEventArgs(currentDataRowIndex, dataRowIndexCount));
                }
            }
            xmlWriter.WriteEndElement();
            
            xmlWriter.WriteEndDocument();

            xmlWriter.Close();
        }
    }
}
